var mongoose = require('mongoose');
var multer  = require('multer');
var path=require('path');
const DataModel = require('../models/upload.model');
// const DateModle = require('../models/user.model');//User模型
//设置存储路径
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './uploads')
    },
    //返回文件名
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
    }
  })
  
//   var upload = multer({ storage: storage })
exports.upload = function(req,res,next){
   //实例化
   var upload=multer({
        storage:storage
   }).single('aaabbb');

   upload(req,res,(err)=>{
        req.file.categoryId=req.params.categoryId;
        const dataModel = new DataModel(req.file);
        dataModel.save()
            .then(data=>{
            res.json(data);
        })
        res.end('File is upload')
   })
   
};
exports.deletes = function(req,res,next){
    var idarr=req.body.idarr;
    if(idarr.length>0){
        var s=idarr.split(",");
        DataModel.remove({_id:{$in:s}})
            .then(data=> {
            res.json({"msg":"success","status":200});
    })
    }else {
        res.json({"msg":"faild","status":404});
    }
}
exports.remove = function(req,res,next){
    const id = req.params.id;
    DataModel.findByIdAndRemove(id,function(err,data){
        res.json(data);
    })

}
exports.lists = function (req, res, next) {
    var ids = req.body.categoryId.split(",");
    if (ids.length > 0) {
        DataModel.find({categoryId: {$in: ids}}).then(data=> {
                res.json(data);
                console.log(data);
            })
    } else {
        res.json({"msg": "请求失败", "status": 404})
    }
}
exports.update = function(req,res,next){
    const id = req.params.id;

    DataModel.findByIdAndUpdate(id, {$set: req.body}, {new:false})
        .then(data=>{
        res.json(data);
})
}
exports.list = function(req,res,next){
  var page = (req.body.page) ? req.body.page : 1;
  var rows = (req.body.rows) ? req.body.rows : 10;
  var queryCondition = {};
  if(req.body.filename && req.body.filename.trim().length>0){
      filename = req.body.filename;
      queryCondition = {
          "filename" : new RegExp(filename, 'i')
      }
  }

DataModel.find(queryCondition,function(err,data){
      if(err){ 
          consoloe.log(err);
      }else{
          res.json(data);
      }
  }).sort({ _id: -1 })
//    DataModel.paginate(queryCondition, {sort: { _id: -1 }, page: parseInt(page), limit: parseInt(rows) }, function(err, result) {
//         // res.json(result)
//         async.map(result.docs,function (news,done) {
//             comment.count({articleId:news._id},function (err,count) {
//                 if(err){
//                     return;
//                 }else {
//                     news.commentCount=count;
//                     done();
//                 }
//             })
//         },function (err) {
//             result.rows = result.docs;
//             delete result.docs;
//             res.json(result);
//         })
//     });
       
}

